"use strict";

const Controller = require("egg").Controller;
const jwt = require("jsonwebtoken");
const Axios = require("axios");
const client_id =
  "3400cccebf48fae463f54a342e0c8a6b3c158a2f44081c6b07f1a767b7875f3c";
const client_secret =
  "bb1a663996368a79cc124af22538cedae86eaaeb47bec94334fec1f5fb3909ca";
const redirect_uri = "http://127.0.0.1:7001/api/oauth/redirect";
//引入验证码
const SvgCaptcha = require("svg-captcha");
class HomeController extends Controller {
  //查找数据
  async GETMock() {
    const { ctx, app } = this;
    const data = await app.mysql.select("data");
    ctx.body = {
      code: 200,
      message: "发送成功",
      data: data,
    };
  }
  async Login() {
    const { ctx, app } = this;
    const { phone, password } = ctx.request.body;
    const data = await app.mysql.get("login", {
      phone,
      password,
    });
    if (!data) {
      ctx.body = {
        code: 200,
        message: "请注册",
      };
    } else {
      const token = jwt.sign({ ...data, password: "" }, "1904A", {
        expiresIn: "24",
      });
      ctx.body = {
        code: 200,
        message: "登录成功",
        token,
      };
    }
  }
  //注册
  async Register() {
    const { ctx, app } = this;
    const { phone, nickname, password = "", avatar, email } = ctx.request.body;
    const data = await app.mysql.insert("login", {
      phone,
      nickname,
      password,
      avatar,
      email,
    });
    if (data.affectedRows) {
      ctx.body = {
        code: 200,
        message: "注册成功",
      };
    } else {
      ctx.body = {
        code: 404,
        message: "注册失败",
      };
    }
  }
  //验证码
  async getCode() {
    const { ctx } = this;
    const options = {
      size: 4,
      fontSize: 50,
      width: 100,
      height: 30,
      color: true,
    };
    const cap = SvgCaptcha.create(options);
    ctx.session.code = cap.text;
    ctx.body = {
      code: 200,
      message: "验证码获取成功",
      data: cap.data,
    };
  }
  //增删改查数据
  async BlogsMock() {
    let { ctx, app } = this;
    let data = await app.mysql.select("blogs");

    ctx.body = {
      code: 200,
      message: "增删改查数据",
      data: data,
    };
  }
  //删除
  async DelMock(){
    const {ctx,app} = this;
    const {id} = ctx.params;
    const data = await app.mysql.query(
      `delete from blogs where id=?`,[id]
    )
    if(data.affectedRows){
      ctx.body = {
        code:200,
        message:"删除成功",
      }
    }else{
      ctx.body = {
        code:404,
        message:"删除失败"
      }
    }
  }
  //搜索
  async Search(){
    const {ctx,app} = this;
    const {val} = ctx.request.body;
    const ipt = `select * from blogs where 1=1 and title like "%${val}%"`;
    const list = await app.mysql.query(ipt);
    ctx.body = {
      list
    }
  }
   //搜索
   async MOCKSearch(){
    const {ctx,app} = this;
    const {val} = ctx.request.body;
    const ipt = `select * from data where 1=1 and name like "%${val}%"`;
    const list = await app.mysql.query(ipt);
    ctx.body = {
      list
    }
  }
   //添加保存
   async AddMock(){
    const {ctx,app} = this;
    const {userId,title,context} = ctx.request.body;
    const data = await app.mysql.query(
      `insert into blogs (userId,title,context) values (?,?,?)`,
      [userId,title,context]
    )
    if(data.affectedRows){
      ctx.body = {
        code:200,
        message:"添加成功",
        data
      }
    }else{
      ctx.body = {
        code:404,
        message:"添加失败"
      }
    }
  }
  //创建数据
  async SETMock(){
    const {ctx,app} = this;
    const {title} = ctx.request.body;
    const data = await app.mysql.query(
      `insert into addmock (title) values (?)`,
      [title]
    )
    if(data.affectedRows){
      ctx.body = {
        code:200,
        message:"保存成功",
        data
      }
    }else{
      ctx.body = {
        code:404,
        message:"保存失败"
      }
    }
  }
  async InertMock() {
    let { ctx, app } = this;
    let data = await app.mysql.select("addMock");

    ctx.body = {
      code: 200,
      message: "保存成功",
      data: data,
    };
  }
  async DelSMock(){
    const {ctx,app} = this;
    const {id} = ctx.params;
    const data = await app.mysql.query(
      `delete from addmock where id=?`,[id]
    )
    if(data.affectedRows){
      ctx.body = {
        code:200,
        message:"删除成功",
      }
    }else{
      ctx.body = {
        code:404,
        message:"删除失败"
      }
    }
  }
}

module.exports = HomeController;
